package br.unicamp.mc715.common;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * This class specializes <code>LinkedHashMap</code> in such way that it
 * has a fixed maximum capacity. This maximum is enforced by removing the
 * least accessed elements as new elements are added. <p>
 *
 * @param <K> the type of keys maintained by this map
 * @param <V> the type of mapped values
 */

public class LRUHashMap<K,V> extends LinkedHashMap<K,V> {
    private static final long serialVersionUID = -6080118320861716818L;
    
    private int maxSize;
    
    public LRUHashMap(int maxSize) {
        super(maxSize * 2, (float) 0.75, true);
        this.maxSize = maxSize;
    }
    
    @Override
    protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
        return size() > maxSize;
    }
}
